/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import React from 'react';
// import {Form,Tabs} from 'antd';
import {Form} from 'antd';
import styles from './login.module.css';
// import envStore from 'pages/config/environment/store';
// import appStore from 'pages/config/app/store';
// import requestStore from 'pages/deploy/request/store';
// import execStore from 'pages/exec/task/store';
// import hostStore from 'pages/host/store';
import PasswordMethod from "./PasswordMethod";
// import PhoneMethod from "./PhoneMethod";
import {getAccessToken, removeCaptchaToken, setAccessToken, removeAccessToken, writeUserInfo} from "../../libs/storage";
import {jump} from "../../libs/funcs";
import '../../libs/config'
import {UserInfo} from "../../api/AccountApi";

class LoginIndex extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loginType: 'password',
      isLogin:this.checkIsLogin()
    };
  }

  /**
   * 检查是否已经登录
   */
  checkIsLogin = () => {
     let accessToken = getAccessToken();
     if (accessToken && accessToken !== "undefined"){
       jump();
       return true;
     }

     return false;
  };

  componentDidMount() {
      //做store的初始化
    // envStore.records = [];
    // appStore.records = [];
    // requestStore.records = [];
    // requestStore.deploys = [];
    // hostStore.records = [];
    // execStore.hosts = [];=
    this.autoLogin();
  }

  // 自动登录 请求地址带上 ?access_token=xxxxxx
  autoLogin = () => {
      if (this.props.location.search){
          let str = this.props.location.search;
          let reg = /.*[&|\?]access_token=([^&]*)(.*)/;
          let res = str.match(reg)
          if (res.indexOf(1) === -1){
              setAccessToken(res[1]);
              UserInfo().then(response => {
                  if (response.data.code === 200){
                      writeUserInfo(response.data.user_info);
                      removeCaptchaToken();
                      jump();
                  }else {
                      removeAccessToken()
                  }
              })
          }

      }
  };


  hangTabChange = (e) => {
     this.setState({
       loginType:e
     })
  };

  render() {
    return (
      <div className={styles.container}>
        <div className={styles.titleContainer}>
          <div className={styles.desc}><h1>{global.app.appName}</h1></div>
        </div>
        <div className={styles.formContainer}>
            <PasswordMethod isLogin={this.state.isLogin}/>
            {/*<Tabs classNam={styles.tabs} onTabClick={this.hangTabChange}>*/}
            {/*    <Tabs.TabPane tab="普通登录" key="password"/>*/}
            {/*    <Tabs.TabPane tab="免密登录" key="phone"/>*/}
            {/*</Tabs>*/}

            {/*{*/}
            {/*    this.state.loginType === "password" ?*/}
            {/*        <PasswordMethod isLogin={this.state.isLogin}/> :*/}
            {/*        <PhoneMethod/>*/}
            {/*}*/}
        </div>
      </div>
    )
  }
}

export default Form.create()(LoginIndex)
